package com.joplus.housekeeper.ui.fragment.xmppCustomer;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.joplus.housekeeper.R;
import com.joplus.housekeeper.base.BaseFragment2;
import com.joplus.housekeeper.bean.CodeBean;
import com.joplus.housekeeper.bean.CustomerNoteBean;
import com.joplus.housekeeper.network.RequestHelper;
import com.joplus.housekeeper.network.TextCallback;
import com.joplus.housekeeper.utils.StringUtils;
import com.joplus.housekeeper.utils.T;


/**
 * Created by zisheng on 2017/9/18.
 */

public class LabelFragment extends BaseFragment2 {

    private String accountId;
    private Button bt;
    private EditText nickName;
    private EditText officeAddress;
    private EditText email;
    private EditText officeName;
    private EditText familyAddress;
    private EditText wxNo;
    private EditText qqNo;
    private EditText accountName;
    private EditText accountNote;
    private String sid = "";

    @Override
    public View initView(LayoutInflater inflater) {
        return inflater.inflate(R.layout.fragment_customer_label, null);
    }

    @Override
    protected void initFindViewById(View view) {
        Bundle arguments = getArguments();
        accountId = arguments.getString("accountId");
        accountNote = (EditText) view.findViewById(R.id.accountNote);
        accountName = (EditText) view.findViewById(R.id.accountName);
        qqNo = (EditText) view.findViewById(R.id.qqNo);
        wxNo = (EditText) view.findViewById(R.id.wxNo);
        officeName = (EditText) view.findViewById(R.id.officeName);
        familyAddress = (EditText) view.findViewById(R.id.familyAddress);
        email = (EditText) view.findViewById(R.id.email);
        officeAddress = (EditText) view.findViewById(R.id.officeAddress);
        nickName = (EditText) view.findViewById(R.id.nickName);
        bt = (Button) view.findViewById(R.id.bt);
    }

    @Override
    public void initData() {
        RequestHelper.getInstance().getNote(getContext(), accountId, new TextCallback() {
            @Override
            public void onFailure(String responseString) {

            }

            @Override
            public void onSuccess(Object responseString) {
                CustomerNoteBean responseString1 = (CustomerNoteBean) responseString;
                if (!responseString1.getCode().equals("2000")) {
                    T.showShort(getContext(), responseString1.getMessage());
                    return;
                }
                sid = responseString1.getData().getMessageCustomerNote().getSid();
                StringUtils.setText(accountNote, responseString1.getData().getMessageCustomerNote().getAccountNote());
                StringUtils.setText(accountName, responseString1.getData().getMessageCustomerNote().getAccountName());
                StringUtils.setText(qqNo, responseString1.getData().getMessageCustomerNote().getQqNo());
                StringUtils.setText(wxNo, responseString1.getData().getMessageCustomerNote().getWxNo());
                StringUtils.setText(email, responseString1.getData().getMessageCustomerNote().getEmail());
                StringUtils.setText(nickName, responseString1.getData().getMessageCustomerNote().getNickName());
                StringUtils.setText(officeAddress, responseString1.getData().getMessageCustomerNote().getOfficeAddress());
                StringUtils.setText(familyAddress, responseString1.getData().getMessageCustomerNote().getFamilyAddress());
                StringUtils.setText(officeName, responseString1.getData().getMessageCustomerNote().getOfficeName());

            }
        });
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CustomerNoteBean.DataBean.MessageCustomerNoteBean messageCustomerNoteBean = new CustomerNoteBean.DataBean.MessageCustomerNoteBean();
                messageCustomerNoteBean.setAccountNote(accountNote.getText().toString());
                messageCustomerNoteBean.setAccountName(accountName.getText().toString());
                messageCustomerNoteBean.setQqNo(qqNo.getText().toString());
                messageCustomerNoteBean.setWxNo(wxNo.getText().toString());
                messageCustomerNoteBean.setEmail(email.getText().toString());
                messageCustomerNoteBean.setNickName(nickName.getText().toString());
                messageCustomerNoteBean.setOfficeAddress(officeAddress.getText().toString());
                messageCustomerNoteBean.setFamilyAddress(familyAddress.getText().toString());
                messageCustomerNoteBean.setOfficeName(officeName.getText().toString());
                messageCustomerNoteBean.setSid(sid);
                String s = new Gson().toJson(messageCustomerNoteBean);
                RequestHelper.getInstance().saveNote(s, new TextCallback() {
                    @Override
                    public void onFailure(String responseString) {

                    }

                    @Override
                    public void onSuccess(Object responseString) {
                        CodeBean responseString1 = (CodeBean) responseString;
                        T.showShort(getContext(), responseString1.getMessage());
                    }
                });

            }
        });
    }
}
